package com.atguigu.auth.service.impl;

import com.atguigu.auth.mapper.SysRoleMapper;
import com.atguigu.auth.service.SysRoleService;
import com.atguigu.auth.service.SysUserRoleService;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysUserRole;
import com.atguigu.vo.system.AssignRoleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * className: SysRoleServiceImpl
 * description:
 * date: 2023/4/11 0011-22:26
 * <p>
 * project: guigu-oa-parent
 * package: com.atguigu.auth.service.impl
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 查询所有角色和当前用户所属角色
     *
     * @param userId {@link Long}
     * @return {@link Map <String, Object>}
     */
    @Override
    public Map<String, Object> findRoleDataByUserId(Long userId) {
        // 1.查询所有角色，返回 list 集合
        List<SysRole> allRoleList = list(null);

        // 2.根据 userid 查询角色用户关系表，查询到 userid 对应所有角色 id
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, userId);
        List<SysUserRole> existUserRoleList = sysUserRoleService.list(wrapper);
        // 从查询出来的用户 id 对应角色 list 集合，获取所有角色 id
        List<Long> existRoleIdList = existUserRoleList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());

        // 3.根据查询所有角色 id，找到对应角色信息
        // 根据角色 id 到所有角色的 list 集合进行比较
        List<SysRole> assingRoleList = allRoleList.stream().map(item -> {
            if (existRoleIdList.contains(item.getId())) {
                return item;
            }
            return null;
        }).filter(item -> !StringUtils.isEmpty(item)).collect(Collectors.toList());

        // 4.把得到两部分数据封装 map 集合，返回
        Map<String, Object> roleMap = new HashMap<>(16);
        roleMap.put("assignRoleList", assingRoleList);
        roleMap.put("allRoleList", allRoleList);
        return roleMap;
    }

    /**
     * 为用户分配角色
     *
     * @param assignRoleVo {@link AssignRoleVo}
     */
    @Override
    public void doAssign(AssignRoleVo assignRoleVo) {
        // 把用户之前分配的角色数据删除，用户角色关系表里面， 根据 userid 删除
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, assignRoleVo.getUserId());
        sysUserRoleService.remove(wrapper);

        // 重新进行分配
        Long userId = assignRoleVo.getUserId();
        List<SysUserRole> sysUserRoleList = assignRoleVo.getRoleIdList().stream().map(item -> {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleId(item);
            return sysUserRole;
        }).collect(Collectors.toList());
        sysUserRoleService.saveBatch(sysUserRoleList);
    }
}
